import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

public class FighterBulletSprite implements Runnable{
    private int maxBullets = 60;
    private int[] bulletX = new int[60];
    private int[] bulletY = new int[60];
    private Thread runner;
    private LayerManager manager;
    public static Sprite[] fBulletSprite = new Sprite[60];
    private Image fBulletImage;
    private Image fBulletImage2;
    private FighterCanvas parent;
    private Sprite planeSprite;
    private int bulletCount = 0;
    private int newX;
    private int totalCount = 0;
    private boolean pressed = false;
    private int count = 0;
    
    //the distance the bullet moves
    private int dx = 6;


    public FighterBulletSprite(FighterCanvas parent){
        this.parent = parent;
        this.manager = parent.getManager();
    }

    public void start(){
        //load image
        try{
            fBulletImage = Image.createImage("/fighterbullet.png");
            fBulletImage2 = Image.createImage("/fighterbullet2.png");
        } catch(Exception e) {System.err.println(e); return; }

        this.planeSprite = parent.getPlaneSprite();

        //start thread to display bullets
        runner = new Thread(this);
        runner.start();
    }

    public void run() {
        try {
                for(int i=0; i<60; i++){
                    fBulletSprite[i] = new Sprite(fBulletImage, 4, 1);
                }
            while(true){
                pressed = parent.checkFire();
                moveBullets();
                if(count%4 == 0){
                    if(pressed==true) {

                        createBullets();

                    }
                }
                count++;
              //wait before creating another one
              Thread.currentThread().sleep(40);

            }
        } catch(Exception e) {System.err.println(e);}
    }

    private void createBullets(){


        if(planeSprite.getRefPixelX() < 163){
            switch(parent.weapon){
                case 0:
                    break;
                case 1:
                    fBulletSprite[bulletCount] = new Sprite(fBulletImage, 4, 1);
                    fBulletSprite[bulletCount].defineReferencePixel(1, 1);
                    //get current position of the plane for bullet start position
                    bulletX[bulletCount] = planeSprite.getRefPixelX();
                    bulletY[bulletCount] = planeSprite.getRefPixelY();

                    //set bullet in front of plane
                    fBulletSprite[bulletCount].setPosition(bulletX[bulletCount], bulletY[bulletCount]);

                    //add to manager
                    manager.insert(fBulletSprite[bulletCount], 0);
            
                    bulletCount++;
                    totalCount++;
            
                    if(bulletCount == maxBullets){
                        bulletCount = 0;
                    }
                    parent.dx = 2;
                    parent.dy = 2;
                    break;
                case 3:
                    parent.dx = 4;
                    parent.dy = 4;
                case 2:
                   fBulletSprite[bulletCount] = new Sprite(fBulletImage, 4, 1);
                    fBulletSprite[bulletCount].defineReferencePixel(1, 1);
                    //get current position of the plane for bullet start position
                    bulletX[bulletCount] = planeSprite.getRefPixelX()-9;
                    bulletY[bulletCount] = planeSprite.getRefPixelY()+5;

                    //set bullet in front of plane
                    fBulletSprite[bulletCount].setPosition(bulletX[bulletCount], bulletY[bulletCount]);

                    //add to manager
                    manager.insert(fBulletSprite[bulletCount], 0);

                    bulletCount++;
                    totalCount++;
                    if(bulletCount == maxBullets){
                        bulletCount = 0;
                    }
                    fBulletSprite[bulletCount] = new Sprite(fBulletImage, 4, 1);
                    fBulletSprite[bulletCount].defineReferencePixel(1, 1);
                    //get current position of the plane for bullet start position
                    bulletX[bulletCount] = planeSprite.getRefPixelX()-6;
                    bulletY[bulletCount] = planeSprite.getRefPixelY()-6;

                    //set bullet in front of plane
                    fBulletSprite[bulletCount].setPosition(bulletX[bulletCount], bulletY[bulletCount]);

                    //add to manager
                    manager.insert(fBulletSprite[bulletCount], 0);

                    bulletCount++;
                    totalCount++;
                    
                    if(bulletCount == maxBullets){
                        bulletCount = 0;
                    }

                    break;
                case 5:
                    //shield

                case 4:
                   fBulletSprite[bulletCount] = new Sprite(fBulletImage, 4, 1);
                    fBulletSprite[bulletCount].defineReferencePixel(1, 1);
                    //get current position of the plane for bullet start position
                    bulletX[bulletCount] = planeSprite.getRefPixelX()-10;
                    bulletY[bulletCount] = planeSprite.getRefPixelY()+6;

                    //set bullet in front of plane
                    fBulletSprite[bulletCount].setPosition(bulletX[bulletCount], bulletY[bulletCount]);

                    //add to manager
                    manager.insert(fBulletSprite[bulletCount], 0);

                    bulletCount++;
                    totalCount++;
                    if(bulletCount == maxBullets){
                        bulletCount = 0;
                    }
                    fBulletSprite[bulletCount] = new Sprite(fBulletImage, 4, 1);
                    fBulletSprite[bulletCount].defineReferencePixel(1, 1);
                    //get current position of the plane for bullet start position
                    bulletX[bulletCount] = planeSprite.getRefPixelX();
                    bulletY[bulletCount] = planeSprite.getRefPixelY()-1;

                    //set bullet in front of plane
                    fBulletSprite[bulletCount].setPosition(bulletX[bulletCount], bulletY[bulletCount]);

                    //add to manager
                    manager.insert(fBulletSprite[bulletCount], 0);

                    bulletCount++;
                    totalCount++;

                    if(bulletCount == maxBullets){
                        bulletCount = 0;
                    }
                    fBulletSprite[bulletCount] = new Sprite(fBulletImage, 4, 1);
                    fBulletSprite[bulletCount].defineReferencePixel(1, 1);
                    //get current position of the plane for bullet start position
                    bulletX[bulletCount] = planeSprite.getRefPixelX()-6;
                    bulletY[bulletCount] = planeSprite.getRefPixelY()-7;

                    //set bullet in front of plane
                    fBulletSprite[bulletCount].setPosition(bulletX[bulletCount], bulletY[bulletCount]);

                    //add to manager
                    manager.insert(fBulletSprite[bulletCount], 0);

                    bulletCount++;
                    totalCount++;

                    if(bulletCount == maxBullets){
                        bulletCount = 0;
                    }
                    parent.dx = 4;
                    parent.dy = 4;
                    break;

            }
        }
        
    }

    private void moveBullets(){
        if(totalCount!=0){
            if(totalCount == bulletCount){
                for(int i=0; i<bulletCount; i++){
                    newX = fBulletSprite[i].getX() + dx;
                    fBulletSprite[i].setPosition(newX, fBulletSprite[i].getY());
                }
            }
            else{
                for(int i=0; i<maxBullets; i++){
                    newX = fBulletSprite[i].getX() + dx;
                    fBulletSprite[i].setPosition(newX, fBulletSprite[i].getY());
                
                    if(fBulletSprite[i].getRefPixelX() > 162){
                        fBulletSprite[i].setVisible(false);
                    }
                }
            }
        }

    }

    public static Sprite getFBulletSprite(int delta){
        return fBulletSprite[delta];
    }

}
